package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.CompanyBusinessFeeConfig;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.holder.RequestHolder;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.CompanyBusinessFeeConfigMapper;
import com.zrwl.poscloud.xo.service.CompanyBusinessFeeConfigService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.CompanyBusinessFeeConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商户业务费用配置 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-09-22
 */
@Service
public class CompanyBusinessFeeConfigServiceImpl extends SuperServiceImpl<CompanyBusinessFeeConfigMapper, CompanyBusinessFeeConfig> implements CompanyBusinessFeeConfigService {

    @Autowired
    private CompanyBusinessFeeConfigService companyBusinessFeeConfigService;

    @Override
    public IPage<CompanyBusinessFeeConfig> getPageList(CompanyBusinessFeeConfigVO companyBusinessFeeConfigVO) {
        QueryWrapper<CompanyBusinessFeeConfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(companyBusinessFeeConfigVO.getKeyword()) && !StringUtils.isEmpty(companyBusinessFeeConfigVO.getKeyword().trim())) {
            queryWrapper.like("name", companyBusinessFeeConfigVO.getKeyword().trim());
        }
        if (companyBusinessFeeConfigVO.getBusinessType() != 0) {
            queryWrapper.eq("business_type", companyBusinessFeeConfigVO.getBusinessType());
        }
        HttpServletRequest request = RequestHolder.getRequest();
        String ownerUid = null;
        if (request.getAttribute(SysConf.OWNER_UID) != null) {
            ownerUid = request.getAttribute(SysConf.OWNER_UID).toString();
        }
        if (companyBusinessFeeConfigVO.getCompanyUid() != null) {
            queryWrapper.eq("company_uid", companyBusinessFeeConfigVO.getCompanyUid());
        } else {
            queryWrapper.eq("company_uid", ownerUid);
        }
        Page<CompanyBusinessFeeConfig> page = new Page<>();
        page.setCurrent(companyBusinessFeeConfigVO.getCurrentPage());
        page.setSize(companyBusinessFeeConfigVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<CompanyBusinessFeeConfig> pageList = companyBusinessFeeConfigService.page(page, queryWrapper);
        List<CompanyBusinessFeeConfig> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<CompanyBusinessFeeConfig> getAllList(CompanyBusinessFeeConfigVO companyBusinessFeeConfigVO) {
        QueryWrapper<CompanyBusinessFeeConfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(companyBusinessFeeConfigVO.getKeyword()) && !StringUtils.isEmpty(companyBusinessFeeConfigVO.getKeyword().trim())) {
            queryWrapper.like("name", companyBusinessFeeConfigVO.getKeyword().trim());
        }
        if (companyBusinessFeeConfigVO.getBusinessType() != 0) {
            queryWrapper.eq("business_type", companyBusinessFeeConfigVO.getBusinessType());
        }
        HttpServletRequest request = RequestHolder.getRequest();
        String ownerUid = null;
        if (request.getAttribute(SysConf.OWNER_UID) != null) {
            ownerUid = request.getAttribute(SysConf.OWNER_UID).toString();
        }
        if (companyBusinessFeeConfigVO.getCompanyUid() != null) {
            queryWrapper.eq("company_uid", companyBusinessFeeConfigVO.getCompanyUid());
        } else {
            queryWrapper.eq("company_uid", ownerUid);
        }
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<CompanyBusinessFeeConfig> list = companyBusinessFeeConfigService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(CompanyBusinessFeeConfigVO companyBusinessFeeConfigVO) {
        CompanyBusinessFeeConfig entity = new CompanyBusinessFeeConfig();
        HttpServletRequest request = RequestHolder.getRequest();
        String ownerUid = null;
        Integer ownerType = null;
        if (request.getAttribute(SysConf.OWNER_UID) != null) {
            ownerUid = request.getAttribute(SysConf.OWNER_UID).toString();
            ownerType = Integer.parseInt(request.getAttribute(SysConf.OWNER_TYPE).toString());
        }
        if (ownerType == 2) {
            companyBusinessFeeConfigVO.setCompanyUid(ownerUid);
        }
        BeanUtils.copyProperties(companyBusinessFeeConfigVO, entity, SysConf.STATUS);
        entity.insert();
        companyBusinessFeeConfigVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(CompanyBusinessFeeConfigVO companyBusinessFeeConfigVO) {
        CompanyBusinessFeeConfig entity = companyBusinessFeeConfigService.getById(companyBusinessFeeConfigVO.getUid());
        BeanUtils.copyProperties(companyBusinessFeeConfigVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(CompanyBusinessFeeConfigVO companyBusinessFeeConfigVO) {
        CompanyBusinessFeeConfig entity = companyBusinessFeeConfigService.getById(companyBusinessFeeConfigVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<CompanyBusinessFeeConfig> getFeeConfigMyCompany(AdminVO adminVO,int businessType) {
        QueryWrapper<CompanyBusinessFeeConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_uid", adminVO.getCompanyUid());
        queryWrapper.eq("business_type", businessType);
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        List<CompanyBusinessFeeConfig> list = companyBusinessFeeConfigService.list(queryWrapper);
        return list;
    }
}
